#ifndef SLOOP_BITMAP_H
#define SLOOP_BITMAP_H

#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
typedef unsigned int u32;
typedef unsigned char u8;
#endif

struct bitmap {
	u32 size;
	u8 shift;
	union {
		u32 bits[32];
		struct bitmap* pbm[32];
	};
};

struct bitmap_save {
	u32 bitmap_size;
	size_t buf_size;
	u8* buf;
};

struct bitmap* bm_alloc(u32 size);
void bm_free(struct bitmap* bm);
int bm_getb(struct bitmap* bm, u32 pos);
int bm_setb(struct bitmap* bm, u32 pos, int val);
int bm_nextb(struct bitmap* bm, u32* pos);
u32 bm_count(struct bitmap* bm);
size_t bm_enc_size(struct bitmap* bm);
size_t bm_enc_buf(struct bitmap* bm, u32* buf, size_t size);
size_t bm_dec_buf(struct bitmap* bm, const u32* buf, size_t size);

#define u32_isset(u, n) (0 != ( (u) & ( 1 << (n) ) ))
#define u32_set_bit(u, n) ( (u) | 1 << (n) )
#define u32_unset_bit(u, n) ( (u) & ~(1 << (n)) )
#define u32_setb(u, n, v) ( (v)? u32_set_bit(u, n) : u32_unset_bit(u, n) )

#endif // SLOOP_BITMAP_H
